package com.yww.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yww.entity.Modules;
import com.yww.entity.RoleModules;
import com.yww.service.IModulesService;
import com.yww.service.IRoleModulesService;
import com.yww.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器 模块控制层
 * </p>
 *
 * @author jobob杨雯雯
 * @since 2020-05-20
 */
@RestController
@RequestMapping("/modules")
public class ModulesController {
    @Autowired
    IModulesService iModulesService;
    @Autowired
    IRoleModulesService iRoleModulesService;

    /**
     * (查)查询所有模块
     * @return
     */
    @RequestMapping("/getModulesAllList")
    public Object getModulesAllList(){
        List<Modules> list = iModulesService.list();
        return list;
    }


    /**
     * (增)添加模块
     * @param modules
     * @return
     */
    @RequestMapping("/addModules")
    public Object addModules(Modules modules){
        Modules mod_name = iModulesService.getOne(new QueryWrapper<Modules>().eq("mod_name", modules.getModName()));
        if(mod_name!=null){
            return new Result(2,"不能存在相同模块");
        }else{
            boolean save = iModulesService.save(modules);
            if(save){
                return new Result(0,"添加成功");
            }else{
                return new Result(1,"添加失败");
            }
        }


    }


    /**
     * (删)根据模块id删除模块
     * @param modId
     * @return
     */
    @RequestMapping("/delModules")
    public Object delModules(@RequestParam Integer modId){
        List<RoleModules> roleByModules = iModulesService.getRoleByModules(modId);
        if(roleByModules.size()>0){
            return new Result(2,"该模块下关联角色，不能删除");
        }else{
            boolean b = iModulesService.removeById(modId);
            if(b){
                return new Result(0,"删除成功");
            }else{
                return new Result(1,"删除失败");
            }
        }


    }


    /**
     * (改)根据模块id进行修改
     * @param modules
     * @return
     */
    @RequestMapping("/updModules")
    public Object updModules(@Valid Modules modules){
        QueryWrapper<Modules> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mod_name", modules.getModName());
        Modules one = iModulesService.getOne(queryWrapper);
        if(one!=null && one.getModId()!=modules.getModId()){
            return new Result(2,"该子模块已存在 不能修改");
        }
        boolean b = iModulesService.updateById(modules);
        if(b){
            return new Result(0,"修改成功");
        }else{
            return new Result(1,"修改失败");
        }
    }


    /**
     * (查)获取模块树
     * @return
     */
    @RequestMapping("/getModulesTree")
    public Object getModulesTree(){
        List<Modules> list=iModulesService.getModulesTree();
        System.out.println(list);
        return list;
    }


    /**
     * (查)根据角色id查询模块树
     * @return
     */
    @RequestMapping("/getModulesTreeByRoleId")
    public Object getModulesTreeByRoleId(Integer roleId){
        List<Modules> list=iModulesService.getModulesTreeByRoleId(roleId);
        return list;

    }


    /**
     * 给指定角色 批量设置拥有的模块
     * param：角色id 模块id组成的字符串
     * @return
     */
    @RequestMapping("/setModulesTreeByRoleId")
    public Object setModulesTreeByRoleId(@RequestParam Integer roleId,@RequestParam String mids){
       //1.删除指定角色的所有数据(中间表)
        QueryWrapper<RoleModules> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("roles_id",roleId);
        iRoleModulesService.remove(queryWrapper);

        //2.给指定角色 批量添加数据     --->(设置的中间表)
        List<RoleModules> list=new ArrayList<>();
        if(mids.length()>0){
            String[] arr = mids.split(",");
            for(String s:arr){
                list.add(new RoleModules(0,roleId,Integer.valueOf(s)));
            }
        }else {
            return new Result(2,"设置成功");
        }

        boolean b = iRoleModulesService.saveBatch(list);
        if(b){
            return new Result(0,"设置成功");
        }else{
            return new Result(1,"设置失败");
        }


    }


    /**
     * 根据模块id查询模块
     * @param modId
     * @return
     */
    @RequestMapping("/getModulesByModId")
    public Object getModulesByModId(Integer modId){
        Modules modules=iModulesService.getModulesByModId(modId);
        if(modules!=null){
            return new Result(0,"查询成功",0,modules);
        }else {
            return new Result(1,"查询失败");
        }

    }


    /**
     * (查)根据用户id查询指定模块...
     * @return
     */
    @RequestMapping("/getModulesTreeByUserId")
    public Object getModulesTreeByUserId(Integer userId){
        List<Modules> list=iModulesService.getModulesTreeByUserId(userId);
        return list;

    }



}
